const a = document.getElementById('addUser')
const addDialog = document.getElementsByClassName('addUser-wrapper')[0]
const updateDialog = document.getElementsByClassName('updateUser-wrapper')[0]
const updateBtn= document.getElementById('updateBtn')

const searchUserBtn = document.getElementsByClassName('searchUserBtn')[0];

const searchUserInput = document.getElementsByClassName('searchUserText')[0];

var cancleBtn = document.getElementsByClassName('cancle');


cancleBtn[0].addEventListener('click',()=>{
    addDialog.style.display = 'none';
});

cancleBtn[1].addEventListener('click',()=>{
    updateDialog.style.display = 'none';
});


a.addEventListener('click',()=>{
    addDialog.style.display = 'block'
})




searchUserBtn.addEventListener('click',()=>{
    var searchUserText = searchUserInput.value;
    location.href = `http://localhost:8080/library/admin/userManage?searchUserText=${searchUserText}`;

})

const deleteUserBtn = document.getElementsByClassName('delete');
for (let i=0;i<deleteUserBtn.length;i++){
    let uid = deleteUserBtn[i].getAttribute('uid');
    deleteUserBtn[i].addEventListener('click',()=>{
        if(confirm(`是否删除id为${uid}的用户`)){
            location.href = `http://localhost:8080/library/admin/deleteUser?uid=${uid}`;
            alert("删除成功！");
        }
        return false;
    });
}

const updateAs = document.getElementsByClassName('update')
for(let i=0;i<updateAs.length;i++){

    updateAs[i].addEventListener('click',()=>{


        let uid = updateAs[i].getAttribute('uid');
        let sno = updateAs[i].getAttribute('sno');
        let name = updateAs[i].getAttribute('name');
        let username = updateAs[i].getAttribute('username');
        let password = updateAs[i].getAttribute('password');
        let email = updateAs[i].getAttribute('email');
        let grade = updateAs[i].getAttribute('grade');
        let phone = updateAs[i].getAttribute('phone');


        updateDialog.style.display = 'block';
        var updateInputs = document.querySelectorAll("#updateForm input")
        updateInputs[0].value =  uid;
        updateInputs[0].setAttribute("disabled","disabled");
        updateInputs[1].value =  sno;
        updateInputs[2].value = name;
        updateInputs[3].value = username;
        updateInputs[4].value = password;
        updateInputs[5].value = phone;
        updateInputs[6].value = grade;
        updateInputs[7].value = email;
        return false;
    });
}
